<?php
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/12/9
 * Time: 11:51
 */

namespace app\admin\logic;

use think\Queue;

/**
 * Class Wechat
 * @package app\admin\logic
 * @author: Huang
 */
class Wechat
{
    use \app\common\traits\Common;

    public static function sendWechatToUser($data)
    {
        $datas = self::HandleData($data);
        $isPushed = Queue::push('app\queue\jobs\Wechat', $datas, $queue = null);

        if ($isPushed !== false) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发送数据参数组装
     * @param $data [
     *          'receive_id'=>接收者
     *          'send_id'=>订单id
     *          'order_id'=>订单id
     *          'temp_id'=>微信模板id
     *          'url'=>微信跳转地址 没有为空
     *          'data'=>[
     *                  'first'=>['value'=>"导航话语",'color'=>'#173177'],
     *                  'keyword1'=>['value'=>'数据','color'=>'#173177'],
     *                  'keyword2'=>['value'=>'数据','color'=>'#173177'],
     *                  'keyword3'=>['value'=> '数据','color'=>'#173177'],
     *                  'keyword4'=>['value'=> '数据','color'=>'#F30808'],
     *                  'keyword5'=>['value'=> '数据','color'=>'#173177']
     *              ]
     *          ]
     * @return array
     * @author: Huang
     */
    public static function HandleData($data)
    {
        $receiveuser = self::getCommonModel()->getOneUserBysId($data['receive_id']);//接收者
        $data['data']['remark'] = ['value' => "感谢使用Gos电商管理系统.", "color" => '#173177'];
        $datas = [
            'touser' => $receiveuser['openid'],//openid
            'template_id' => $data['temp_id'],
            'url' => $data['url'],
            'data' => $data['data']//传参
        ];
        $datas['send_id'] = $data['send_id'];
        $datas['order_id'] = $data['order_id'];
        return $datas;
    }


    /**
     *推送消息到门店店长
     * @param $orderid
     */
    public static function sendWechatToDz($orderid)
    {
        $orderdata = \app\admin\model\Order::where(['id' => $orderid])->find();//获取订单
        $storeuserinfo = collect(self::getCommonModel()->getUserByDeparWx($orderdata['mendian']))->filter(function ($item) {
            if ($item['position'] == 2) {
                return $item;
            }
        });
        $yyuser = self::getCommonModel()->getOneUserById($orderdata['yykf']);
        foreach ($storeuserinfo as $value) {
            $datas = [
                'receive_id' => $value['id'],
                'send_id' => session('admin_user.adminid'),
                'order_id' => $orderid,
                'temp_id' => 'unrRmyZtxF_8zRh_wT1kLXf85LnTTS8eub5hIofFoRY',
                'url' => url('admin/business/dianzhang', ['order_id' => $orderid], 'html', TRUE),
                'data' => [
                    'first' => ['value' => "邀约【{$yyuser['username']}】分配了一个新的订单，请及时分配接待门市！", 'color' => '#173177'],
                    'keyword1' => ['value' => $orderdata['name_z'], 'color' => '#173177'],
                    'keyword2' => ['value' => $orderdata['phone_z'], 'color' => '#173177'],
                    'keyword3' => ['value' => $orderdata['states_text'], 'color' => '#173177'],
                    'keyword4' => ['value' => $orderdata['yyjdtime'], 'color' => '#F30808'],
                    'keyword5' => ['value' => clearRichText($orderdata['yybz']), 'color' => '#173177'],
                ]
            ];
            self::sendWechatToUser($datas);
        }
    }

    /**
     *推送消息到门店门市
     * @param $orderid
     */
    public static function sendWechatToMenshi($orderid)
    {
        $orderdata = \app\admin\model\Order::where(['id' => $orderid])->find();//获取订单
        $datas = [
            'receive_id' => $orderdata['menshi'],
            'send_id' => session('admin_user.adminid'),
            'order_id' => $orderid,
            'temp_id' => 'unrRmyZtxF_8zRh_wT1kLXf85LnTTS8eub5hIofFoRY',
            'url' => url('admin/business/menshi', ['order_id' => $orderid], 'html', TRUE),
            'data' => [
                'first' => ['value' => "您收到一个接待任务！", 'color' => '#173177'],
                'keyword1' => ['value' => $orderdata['name_z'], 'color' => '#173177'],
                'keyword2' => ['value' => $orderdata['phone_z'], 'color' => '#173177'],
                'keyword3' => ['value' => $orderdata['states_text'], 'color' => '#173177'],
//                'keyword4' => ['value' => time_format($orderdata['yyjdtime']), 'color' => '#F30808'],
                'keyword4' => ['value' => $orderdata['yyjdtime'], 'color' => '#F30808'],
                'keyword5' => ['value' => clearRichText($orderdata['yybz']), 'color' => '#173177'],
            ]
        ];
        self::sendWechatToUser($datas);
    }

    /**
     *推送消息到调度
     * @param $orderid
     */
    public static function sendWechatToDiaodu($orderid)
    {
        $orderdata = \app\admin\model\Order::where(['id' => $orderid])->find();//获取订单
        $storeuserinfo = collect(self::getCommonModel()->getUserByDeparWx($orderdata['mendian']))->filter(function ($item) {
            if ($item['position'] == 5) {
                return $item;
            }
        });
        $yyuser = self::getCommonModel()->getOneUserById($orderdata['yykf']);
        foreach ($storeuserinfo as $value) {
            $datas = [
                'receive_id' => $value['id'],
                'send_id' => session('admin_user.adminid'),
                'order_id' => $orderid,
                'temp_id' => 'unrRmyZtxF_8zRh_wT1kLXf85LnTTS8eub5hIofFoRY',
                'url' => url('admin/business/diaodu', ['order_id' => $orderid], 'html', TRUE),
                'data' => [
                    'first' => ['value' => "邀约【{$yyuser['username']}】分配了一个新的拍摄订单，请及时分配摄影师！", 'color' => '#173177'],
                    'keyword1' => ['value' => $orderdata['name_z'], 'color' => '#173177'],
                    'keyword2' => ['value' => $orderdata['phone_z'], 'color' => '#173177'],
                    'keyword3' => ['value' => $orderdata['states_text'], 'color' => '#173177'],
//                    'keyword4' => ['value' => time_format($orderdata['yytime']), 'color' => '#F30808'],
                    'keyword4' => ['value' => $orderdata['yypstime'], 'color' => '#F30808'],
                    'keyword5' => ['value' => clearRichText($orderdata['yybz']), 'color' => '#173177'],
                ]
            ];
            self::sendWechatToUser($datas);
        }
    }

    /**
     * 推送消息到邀约门市
     * @param $orderid
     */
    public static function sendWechatToYyry($orderid)
    {
        $orderdata = \app\admin\model\Order::where(['id' => $orderid])->find();//获取订单
        $datas = [
            'receive_id' => $orderdata['yykf'],
            'send_id' => session('admin_user.adminid'),
            'order_id' => $orderid,
            'temp_id' => 'unrRmyZtxF_8zRh_wT1kLXf85LnTTS8eub5hIofFoRY',
            'url' => '',
            'data' => [
                'first' => ['value' => "您收到一个邀约任务！", 'color' => '#173177'],
                'keyword1' => ['value' => $orderdata['name_z'], 'color' => '#173177'],
                'keyword2' => ['value' => $orderdata['phone_z'], 'color' => '#173177'],
                'keyword3' => ['value' => $orderdata['states_text'], 'color' => '#173177'],
//                'keyword4' => ['value' => $orderdata['yyfptime'], 'color' => '#F30808'],
                'keyword4' => ['value' => '', 'color' => '#F30808'],
                'keyword5' => ['value' => $orderdata['qdbz'], 'color' => '#173177'],
            ]
        ];
        self::sendWechatToUser($datas);
    }

    /**
     * 推送消息到摄影师
     * @param $orderid
     */
    public static function sendWechatToSheyingshi($orderid)
    {
        $orderdata = \app\admin\model\Order::where(['id' => $orderid])->find();//获取订单
        $datas = [
            'receive_id' => $orderdata['photo_grapher'],
            'send_id' => session('admin_user.adminid'),
            'order_id' => $orderid,
            'temp_id' => 'unrRmyZtxF_8zRh_wT1kLXf85LnTTS8eub5hIofFoRY',
            'url' => '',
            'data' => [
                'first' => ['value' => "您收到一个拍摄任务！", 'color' => '#173177'],
                'keyword1' => ['value' => $orderdata['name_z'], 'color' => '#173177'],
                'keyword2' => ['value' => $orderdata['phone_z'], 'color' => '#173177'],
                'keyword3' => ['value' => $orderdata['states_text'], 'color' => '#173177'],
                'keyword4' => ['value' => $orderdata['yypstime'], 'color' => '#F30808'],
                'keyword5' => ['value' => "拍摄地址【".$orderdata['waipai_address']."】，".$orderdata['msbz'], 'color' => '#173177'],
            ]
        ];
        self::sendWechatToUser($datas);
    }
}